package app.fugema.shop.main.goods.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.netBeanPackage.BaseResponse;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.RxTimer;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyLinearLayoutManager;
import com.base.common.view.adapter.connector.OnItemClickInterface;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;
import app.fugema.shop.R;
import app.fugema.shop.databinding.GroupBookingActivityBinding;
import app.fugema.shop.main.goods.adapter.GroupBookingAdapter;
import app.fugema.shop.main.order.model.GroupBookingBean;
import app.fugema.shop.main.order.vm.GroupBookingVM;

public class GroupBookingActivity extends BaseTitleActivity<GroupBookingActivityBinding, GroupBookingVM> implements OnGetDataListener {

    public static void start(Context context) {
        context.startActivity(new Intent(context, GroupBookingActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group_booking_activity);

    }

    @Override
    protected int getStatusBarColor() {
        return R.color.C_F9F9F9;
    }

    private GroupBookingAdapter bookingAdapter;

    private RxTimer rxTimer;

    @Override
    protected void initView() {
        super.initView();
        setTitle("火热拼团");

        bookingAdapter = new GroupBookingAdapter();
        bookingAdapter.setOnItemClickInterface(new OnItemClickInterface() {
            @Override
            public boolean onItemClick(View view, int onclickType, int itemType, int position, Object bean) {
                if (bean instanceof GroupBookingBean.RecordsBean) {
                    if (view.getId() == R.id.tvAdd) {
                        GroupBookingBean.RecordsBean recordsBean = (GroupBookingBean.RecordsBean) bean;
                        UIUtils.showToastSafes("开始拼团");
                        return true;
                    }

                }
                return false;
            }
        });
        binding.rvRecyclerView.setLayoutManager(new MyLinearLayoutManager(this));
        binding.rvRecyclerView.setAdapter(bookingAdapter);
        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);


        rxTimer = new RxTimer();
        rxTimer.interval(1000, new RxTimer.RxAction() {
            @Override
            public void action(long number) {
                if (bookingAdapter.getChildCount() > 0) {
                    for (int i = 0; i < bookingAdapter.getItemCount(); i++) {
                        if (bookingAdapter.getItemBean(i) instanceof GroupBookingBean.RecordsBean) {
                            GroupBookingBean.RecordsBean bean = (GroupBookingBean.RecordsBean) bookingAdapter.getItemBean(i);
                            bean.timeChanged();
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (rxTimer != null) {
            rxTimer.cancel();
        }
    }

    @Override
    protected void initData(Intent intent) {
        super.initData(intent);
        onGetData(1);
    }

    @Override
    public void onGetData(int pn) {
        viewModel.pageGroupOnProd(pn).observe(this, new BaseViewObserver<LiveDataWrapper<GroupBookingBean>>(viewUtils.getStateLayout()) {
            @Override
            protected void onSuccess(LiveDataWrapper<GroupBookingBean> data) {
                viewUtils.setDataListRefreshLayout(bookingAdapter, data.data.getData().getCurrent(), data.data.getData().getSize(), data.data.getData().getRecords());
//                if (pn == 1) {
//                    List list = new ArrayList();
//                    list.add(new ADInfo());
//                    list.addAll(data.data.getData().getRecords());
//                    viewUtils.setDataListRefreshLayout(bookingAdapter, 1, data.data.getData().getSize(), list);
//                } else {
//                    viewUtils.setDataListRefreshLayout(bookingAdapter, data.data.getData().getCurrent(), data.data.getData().getSize(), data.data.getData().getRecords());
//                }
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<GroupBookingBean> data) {
                return data.data.getData().getCurrent() == 1 && UIUtils.isEmpty(data.data.getData().getRecords());
            }

            @Override
            public boolean isRefreshLayoutData() {
                return true;
            }
        });
    }
}
